package com.lunaler.stream;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.pooled.DbConfig;
import cn.hutool.db.ds.pooled.PooledDataSource;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Date;

public class JdbcUtils {
    private static Db db;
    //private Date
    static  {
        // url: jdbc:mysql://124.71.211.223:3313/spider?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        //      username: admin
        //      password: admin@987@
        DbConfig config = new DbConfig();
        config.setUrl("jdbc:mysql://124.71.211.223:3313/spider?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai");
        config.setUser("admin");
        config.setPass("admin@987@");
        config.setMaxActive(10);
        PooledDataSource ds = new PooledDataSource(config);
        db = new Db(ds);
    }

    public static void saveOrUpdate(long time, String app) throws Exception {
        Date ctime = new Date(time);
        String timeStr = DateFormatUtils.format(ctime, "yyyy-MM-dd");
       Entity old = db.get(Entity.create("core.ads_uv_log").set("curr_date", timeStr).set("app", app));
       if (old == null) {
           old = Entity.create("core.ads_uv_log");
           old.set("curr_date", timeStr);
           old.set("cnt", 1);
           old.set("app", app);
           db.insert(old);
       } else {
           old.setTableName("core.ads_uv_log");
           old.set("curr_date", timeStr);
           old.set("cnt", old.get("cnt", 1) + 1);
           old.set("app", app);
           db.update(old, Entity.create("core.ads_uv_log").set("curr_date", timeStr).set("app", app));
       }
       //db.insertOrUpdate(old, "curr_date", "app");
    }


}
